/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jetradar.multibackstack;

import com.jetradar.multibackstack.bean.Action;
import com.jetradar.multibackstack.minterface.CallBack;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;

import java.util.ArrayList;
import java.util.List;

/**
 * BaseAbilitySlice
 *
 * @since 2021-06-10
 */
public abstract class BaseAbilitySlice extends AbilitySlice implements CallBack {
    private static final int NUM_2 = 2;
    protected List<Object> list = new ArrayList<>();

    /**
     * addAction
     *
     * @param fraction fraction
     * @param item     item
     */
    @Override
    public void addAction(Fraction fraction, int item) {
        Action action1 = new Action(fraction, item);
        for (int index = list.size() - 1; index >= 0; index--) {
            if (list.size() - NUM_2 >= 0) {
                Action action = (Action) list.get(list.size() - 1);
                if (action.equals(action1)) {
                    return;
                }
                Action action3 = (Action) list.get(list.size() - NUM_2);
                if (action3.equals(action1)) {
                    list.remove(list.size() - NUM_2);
                    list.add(action1);
                }
            }
        }
        list.add(action1);
    }

    /**
     * pushFragmentToBackStack
     *
     * @param hostId   hostId
     * @param fraction fraction
     * @return boolean
     */
    protected boolean pushFragmentToBackStack(int hostId, Fraction fraction) {
        return list.add(new Action(fraction, hostId));
    }

    /**
     * resetBackStackToRoot
     *
     * @param hostId hostId
     * @return boolean
     */
    protected boolean resetBackStackToRoot(int hostId) {
        Object remove = list.remove(hostId);
        if (remove != null) {
            return true;
        }
        return false;
    }

    /**
     * protected
     *
     * @param hostId clearBackStack
     * @return boolean
     */
    protected boolean clearBackStack(int hostId) {
        Object remove = list.remove(hostId);
        if (remove != null) {
            return true;
        }
        return false;
    }

    /**
     * backStackSize
     *
     * @param hosid hosid
     * @return int
     */
    protected int backStackSize(int hosid) {
        return list.size();
    }

    /**
     * getList
     *
     * @return List<Object> 返回List数据
     */
    protected List<Object> getList() {
        return list;
    }

    /**
     * clear
     *
     * @param hostId hostId
     * @return boolean
     */
    public boolean clear(int hostId) {
        list.clear();
        if (list.size() == 0) {
            return false;
        }
        return true;
    }
}